package com.dataart.zipbackup;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipOutputStream;

public class GzipProcessor {
// Just a regular comment
	// Another regular comment
	public void processGzip(String entryName, InputStream inputStream,
			TextProcessor textProcessor, OutputStream out) throws IOException {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int abc;
			byte[] buf = new byte[1042];
			while ((abc = inputStream.read(buf)) != -1){
				baos.write(buf,0,abc);
			}
			GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(baos.toByteArray()));
			GZIPOutputStream gzos = new GZIPOutputStream(out);
			if (entryName.endsWith(".zip.gz")) {
				ZipProcessor newZip = new ZipProcessor();
				ZipOutputStream zos;
				zos = newZip.processZip(textProcessor, gzis, gzos);
				zos.finish();
			} else {
				if (entryName.endsWith(".gz.gz")) {
					GzipProcessor gzipProc = new GzipProcessor();
					String gzippedFileName = entryName.substring(0,
							(entryName.length() - 3));
					gzipProc.processGzip(gzippedFileName, gzis,
							textProcessor, gzos);
				} else if (entryName.endsWith(".txt.gz")) {
					textProcessor.processText(gzis, gzos);
				} else {
					OtherProcessor otherFiles = new OtherProcessor();
					otherFiles.pocessOther(gzis,gzos);
				}
			}
			gzos.finish();
	}
}
